package me.jiatao.kotlinmall.ui.fragment

import android.view.View
import cn.bingoogolapple.bgabanner.BGABanner
import me.jiatao.baselibrary.ui.adapter.BGABannerAdapter
import me.jiatao.baselibrary.ui.fragment.BaseMvpListFragment
import me.jiatao.baselibrary.utils.NetWorkUtil
import me.jiatao.kotlinmall.R
import me.jiatao.kotlinmall.data.protocol.Article
import me.jiatao.kotlinmall.data.protocol.ArticleResponseBody
import me.jiatao.kotlinmall.data.protocol.Banner
import me.jiatao.kotlinmall.injection.component.DaggerHomeComponent
import me.jiatao.kotlinmall.mvp.presenter.HomePresenter
import me.jiatao.kotlinmall.mvp.view.HomeView
import me.jiatao.kotlinmall.ui.adapter.HomeAdapter


class HomeFragment : BaseMvpListFragment<HomeView, HomePresenter>(), HomeView {
    companion object {
        fun getInstance(): HomeFragment = HomeFragment()
    }

    private lateinit var bannerView: BGABanner
    private val bannerViewAdapter by lazy { BGABannerAdapter(mActivity) }

    /**
     * 文章列表
     */
    private val articleList = mutableListOf<Article>()
    /**
     * 文章列表 Adapter
     */
    private val homeAdapter: HomeAdapter by lazy {
        HomeAdapter(articleList)
    }


    override fun attachLayoutRes(): Int = R.layout.fragment_home

    /**
     * 依赖注入
     */
    override fun injectComponent() {
        DaggerHomeComponent.builder()
            .activityComponent(mActivityComponent)
            .build()
            .inject(this)
    }


    override fun initView(view: View) {
        super.initView(view)

        val bannerLayout = layoutInflater.inflate(R.layout.item_home_banner, null)
        bannerView = bannerLayout.findViewById(me.jiatao.baselibrary.R.id.bga_banner)
        bannerView.setAdapter(bannerViewAdapter)

        recyclerView?.adapter = homeAdapter.apply {
            loadMoreModule?.setOnLoadMoreListener(onRequestLoadMoreListener)
            addHeaderView(bannerLayout)
        }
    }


    override fun onStart() {
        super.onStart()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun lazyLoad() {
        mLayoutStatusView?.showLoading()
        mPresenter.requestHomeData()
    }


    override fun onRefreshList() {
        mPresenter.requestHomeData()
    }

    override fun onLoadMoreList() {
        if (!NetWorkUtil.isNetworkConnected(mActivity)) {
            homeAdapter.loadMoreModule?.loadMoreFail()
            showToast("网络连接失败,请检查网络")
        }

        val page = homeAdapter.data.size / 20
        mPresenter.requestArticles(page)
    }


    override fun scrollToTop() {
        recyclerView?.run {

            //            if (recyclerView?.layoutManager.findFirstVisibleItemPosition() > 20) {
//                scrollToPosition(0)
//            } else {
//                smoothScrollToPosition(0)
//            }

            smoothScrollToPosition(0)
        }
    }

    /**
     * Banner数据获取成功后回调
     */
    override fun setBanner(banners: List<Banner>) {
        if (banners.isNotEmpty()) {
            bannerViewAdapter.clearBannerList()

            banners.forEach { banner ->
                bannerViewAdapter.addBannerItem(banner.imagePath, banner.title)
            }
            /**
             * 设置是否开启自动轮播，需要在 setData 方法之前调用，并且调了该方法后必须再调用一次 setData 方法
             * 例如根据图片当图片数量大于 1 时开启自动轮播，等于 1 时不开启自动轮播
             */
            /**
             * 设置是否开启自动轮播，需要在 setData 方法之前调用，并且调了该方法后必须再调用一次 setData 方法
             * 例如根据图片当图片数量大于 1 时开启自动轮播，等于 1 时不开启自动轮播
             */
            bannerView.setAutoPlayAble(bannerViewAdapter.getItemSize() > 1)
            bannerView.setData(bannerViewAdapter.imgUrlList, bannerViewAdapter.tipList)
        }
    }

    override fun setArticles(articleResponseBody: ArticleResponseBody) {
        articleResponseBody.datas.let { itemList ->

            if (itemList.size > 0) {
                //下拉刷新清空所有数据
                if (isRefresh) {
                    articleList.clear()
                }
                articleList.addAll(itemList)
                homeAdapter.setDiffNewData(articleList)


                val size = itemList.size
                if (size < articleResponseBody.size) {
                    homeAdapter.loadMoreModule?.loadMoreEnd(isRefresh)
                } else {
                    homeAdapter.loadMoreModule?.loadMoreComplete()
                }
            }
        }


        if (homeAdapter.itemCount == 0) {
            mLayoutStatusView?.showEmpty()
        } else {
            mLayoutStatusView?.showContent()
        }
    }
}